{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datasketch import MinHash,  MinHashLSHForest\n",
    "data1 = ['这个', '程序', '代码', '太乱', '那个', '代码', '规范']\n",
    "data2 = ['这个', '程序', '代码', '不', '规范', '那个', '更', '规范']\n",
    "data3 = ['这个', '程序', '代码', '不', '规范', '那个', '规范', '些']\n",
    "#造了三个已经分好词了句子，并用list存储"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.minhash.MinHash object at 0x7fdf34adb7d0> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7fdf34adb790> <class 'datasketch.minhash.MinHash'>\n",
      "<datasketch.minhash.MinHash object at 0x7fdf34adb810> <class 'datasketch.minhash.MinHash'>\n"
     ]
    }
   ],
   "source": [
    "# 创建MinHash对象\n",
    "m1 = MinHash()\n",
    "m2 = MinHash()\n",
    "m3 = MinHash()\n",
    "for d in data1:\n",
    "    m1.update(d.encode('utf8'))\n",
    "for d in data2:\n",
    "    m2.update(d.encode('utf8'))\n",
    "for d in data3:\n",
    "    m3.update(d.encode('utf8'))\n",
    "print(m1,type(m1))    \n",
    "print(m2,type(m2))  \n",
    "print(m3,type(m3))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<datasketch.lshforest.MinHashLSHForest object at 0x7fdf34ad8fd0> <class 'datasketch.lshforest.MinHashLSHForest'>\n"
     ]
    }
   ],
   "source": [
    "# 创建LSH Forest 并往这个数据库里面加内容\n",
    "forest = MinHashLSHForest()\n",
    "forest.add(\"m2\", m2)\n",
    "forest.add(\"m3\", m3)#加入了m2 m3 暂时没有加m1 相加也是可以加的\n",
    "print(forest,type(forest))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'NoneType'>\n"
     ]
    }
   ],
   "source": [
    "# 在检索前，需要使用index\n",
    "forest.index()\n",
    "print(type(forest.index()))#没有数据类型 只是单纯的使用前用一下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 判断forest是否存在m2, m3\n",
    "print(\"m2\" in forest)\n",
    "print(\"m3\" in forest)\n",
    "print(\"m1\" in forest)#同时可以看看m1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top 2 邻居 ['m2', 'm3']\n"
     ]
    }
   ],
   "source": [
    "# 查询forest中与m1相似的Top-K个邻居\n",
    "result = forest.query(m1, 2)\n",
    "print(\"Top 2 邻居\", result)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
